package com.pond.pond.validation;

import com.pond.pond.domain.PondSeat;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PondSeatValidator implements ConstraintValidator<ValidPondSeat, PondSeat> {

    @Override
    public boolean isValid(PondSeat value, ConstraintValidatorContext context) {
        if (value == null) {
            return true; // 交给 @NotNull 控制
        }

        boolean isBatch = Boolean.TRUE.equals(value.getIsBatch());

        if (isBatch) {
            if (value.getBatchCount() == null || value.getCodePrefix() == null) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("批量新增时 batchCount 和 codePrefix 为必填")
                        .addConstraintViolation();
                return false;
            }
        } else {
            if (value.getCode() == null || value.getCode().trim().isEmpty()) {
                context.disableDefaultConstraintViolation();
                context.buildConstraintViolationWithTemplate("非批量新增时 code 为必填")
                        .addConstraintViolation();
                return false;
            }
        }

        return true;
    }
}
